class Solution {
    public int lengthOfLongestSubstring(String s) {
        int[] map = new int[26];
        // 从左往右遍历维护一个窗口
        // 遇到值分两种情况:   
        // 1. 没有在map中，加入窗口，len+1
        // 2. 在map中，依次删除窗口中元素，直到无重复元素
        int left = 0, right = 0;
        int maxLen = 0, tmpLen = 0;
        while (right < s.length()) {
            char c = s.charAt(right);
            if (map[c - 'a'] == 0) {
                map[c - 'a']++;
                tmpLen++;
            } else {
                map[c - 'a']++;
                char t;
                while (map[c - 'a'] > 1) {
                    t = s.charAt(left++);
                    tmpLen--;
                    map[t - 'a']--;
                }
                tmpLen++;
            }
            maxLen = Math.max(maxLen, tmpLen);
            right++;
        }
        return maxLen;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.lengthOfLongestSubstring("pwwkew"));
    }
}